package com.example.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;

@WebServlet(name = "FeedbackServlet", urlPatterns = "/feedback")
public class FeedbackServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String content = request.getParameter("content");
        if (content == null || content.trim().isEmpty()) {
            request.setAttribute("error", "反馈内容不能为空");
            request.getRequestDispatcher("feedback.jsp").forward(request, response);
            return;
        }
        try (Connection conn = com.example.util.DBUtil.getConnection();
             PreparedStatement ps = conn.prepareStatement("INSERT INTO feedback (user_id, content) VALUES (?, ?)");) {
            Integer userId = (Integer) request.getSession().getAttribute("user_id");
            if (userId == null) userId = 0;
            ps.setInt(1, userId);
            ps.setString(2, content);
            ps.executeUpdate();
            request.setAttribute("success", "反馈提交成功，感谢您的参与！");
        } catch (Exception e) {
            request.setAttribute("error", "反馈提交失败: " + e.getMessage());
        }
        request.getRequestDispatcher("feedback.jsp").forward(request, response);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.getRequestDispatcher("feedback.jsp").forward(req, resp);
    }
} 